package alfred.proctice.com.recycleview;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;

import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import alfred.proctice.com.recycleviewother.DymanicAdapter;
import alfred.proctice.com.recycleviewother.HeaderAndFooterWrapper;
import alfred.proctice.http.BaseResponse;
import alfred.proctice.http.GsonUtils;
import alfred.proctice.http.HttpConfig;
import alfred.proctice.http.OkHttpManager;
import me.drakeet.multitype.Items;
import me.drakeet.multitype.MultiTypeAdapter;

public class MainActivity extends AppCompatActivity {
    private RefreshRecycleView recyclerView;
    private DymanicAllBean allBean;
    private int pagerlimit = 5;
    private int pageroffset = 0;
    private String token;
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    topicList(msg.obj.toString());
                    break;
                case 1:
                    allBean = (DymanicAllBean) msg.obj;
/*                    Items Items = new Items();
                    Items.add(new Object());
                    Items.addAll(allBean.list);
                    initRecycle(Items);*/
                    initRecycleAnother(allBean.list);
                    break;
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView = findViewById(R.id.recyclerView);
        Login();
    }


    private void initRecycle(Items items){
        MultiTypeAdapter adapter = new MultiTypeAdapter(items);
        adapter.register(Object.class,new TopFreshBinder());
        adapter.register(DymanicAllBean.Dymanic.class,new DymanicBinder());
        recyclerView.setAdapter(adapter);
    }

    private void initRecycleAnother(List<DymanicAllBean.Dymanic> list){
        RecyclerView.Adapter adapter = new DymanicAdapter(list);
        recyclerView.setAdapterWithBoth(adapter);
        recyclerView.setOnRefreshListener(new RefreshRecycleView.onRefreshListener() {
            @Override
            public void rereshing() {
                pagerlimit = 1;
                pageroffset = 0;
                topicList(token);
            }
        });
        recyclerView.setOnLoadMoreListener(new RefreshRecycleView.onLoadMoreListener() {
            @Override
            public void loading() {
                topicList(token);
            }
        });
    }

    private void Login(){
        Map<String,String> param = new HashMap();
        param.put("phoneNumber", "10000000007");
        param.put("password", "123456");
        OkHttpManager.httpPost(HttpConfig.LOGIN, OkHttpManager.appendPOST(OkHttpManager.JSON, param), new OkHttpManager.OnResultListener() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    token = jsonObject.getJSONObject("data").getString("token");
                    Message msg = new Message();
                    msg.what = 0;
                    msg.obj = token;
                    handler.sendMessage(msg);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Exception e) {

            }
        });
    }

    private void topicList(String token){
        Map<String,String> param = new HashMap();
        param.put("pagerLimit", pagerlimit+"");
        param.put("pagerOffset", pageroffset+"");
        OkHttpManager.httpPost(HttpConfig.topicList, OkHttpManager.appendTokenPOST(OkHttpManager.JSON, param,token), new OkHttpManager.OnResultListener() {
            @Override
            public void onSuccess(String result) {
                Type type = new TypeToken<BaseResponse<DymanicAllBean>>(){}.getType();
                BaseResponse<DymanicAllBean> response = (BaseResponse<DymanicAllBean>) GsonUtils.fromJson(result,type);
                Message msg = new Message();
                msg.what = 1;
                msg.obj = response.getData();
                handler.sendMessage(msg);
                pageroffset += pagerlimit;
                recyclerView.completeRefresh();
            }

            @Override
            public void onFailure(Exception e) {

            }
        });
    }

}
